package com.bicycle.rentalsystem.servelt.admin;

import com.bicycle.rentalsystem.pojo.admin.Administrators;
import com.bicycle.rentalsystem.pojo.admin.Page;
import com.bicycle.rentalsystem.service.admin.AdminService;
import com.bicycle.rentalsystem.service.impl.admin.AdminServiceImpl;
import com.bicycle.rentalsystem.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Third uncle
 * @date 2022/11/22 - 14:14
 */
@WebServlet("/adminServlet")
public class AdminServlet extends BaseServlet {
    AdminService adminService = new AdminServiceImpl();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.查询全部管理员
        List<Administrators> list = adminService.getAdmins();
        System.out.println(list);
        //2.把全部管理员保存到request域中
        req.setAttribute("list", list);
        System.out.println(list);
        //3.请求转发
        req.getRequestDispatcher("admin/a3.jsp").forward(req, resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取要删除的id
        String id = req.getParameter("id");
        int aId = Integer.parseInt(id);
        //2.数据库中删除
        adminService.deleteAdmin(aId);
        //3.重定向回去
        resp.sendRedirect(req.getContextPath()+"/adminServlet?action=page");
    }

    protected void getAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取id
        String id = req.getParameter("id");
        int aId = Integer.parseInt(id);
        //得到次管理员信息
        Administrators admin = adminService.getAdminById(aId);
        //3.保存到requst域中
        req.setAttribute("admin", admin);
        //4.请求转发到修改页面
        req.getRequestDispatcher("admin/updateAdmin.jsp").forward(req, resp);
    }

    protected void updateAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数
        String yonghuming = req.getParameter("yonghuming");
        String mima = req.getParameter("mima");
        String xingbie = req.getParameter("xingbie");
        String id = req.getParameter("id");
        Integer aId = Integer.valueOf(id);
        Administrators admin = new Administrators(aId, yonghuming, mima, xingbie);
        System.out.println(admin);
        adminService.updateAdmin(admin);
        //重定向回去
        resp.sendRedirect(req.getContextPath()+"/adminServlet?action=page");

    }

    /*
    处理分页
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String pageNo = req.getParameter("pageNo");
        /*if(pageNo==null||pageNo==""){
            pageNo="1";
        }
        Integer pageNo2= Integer.valueOf(pageNo);*/
        int pageNo2 = WebUtil.parseInt(pageNo, 1);
        int pageSize = WebUtil.parseInt(req.getParameter("pageSize"), 4);
        //获取page对象
        Page<Administrators> page = adminService.page(pageNo2, 4);
        //保存到request域中
        req.setAttribute("page",page);
        //请求转发
        req.getRequestDispatcher("admin/a3.jsp").forward(req,resp);
    }

}
